package com.atguigu.system.service.impl;

import com.atguigu.common.helper.MenuHelper;
import com.atguigu.common.helper.RouterHelper;
import com.atguigu.model.system.SysMenu;
import com.atguigu.model.system.SysUser;
import com.atguigu.model.vo.RouterVo;
import com.atguigu.model.vo.SysUserQueryVo;
import com.atguigu.system.mapper.SysMenuMapper;
import com.atguigu.system.mapper.SysUserMapper;
import com.atguigu.system.service.SysUserService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements SysUserService {

    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private SysMenuMapper sysMenuMapper;

    @Override
    public IPage<SysUser> findPage(Page<SysUser> page, SysUserQueryVo userQueryVo) {
        return sysUserMapper.findPage(page,userQueryVo);
    }

    @Override
    public void updateStatus(Long userId, Integer status) {
        //创建SysUser对象
        SysUser sysUser = new SysUser();
        //设置id
        sysUser.setId(userId);
        //设置用户状态
        sysUser.setStatus(status);
        //调用SysUserMapper中更新的方法
        sysUserMapper.updateById(sysUser);
    }

    @Override
    public SysUser getSysUserByUsername(String username) {
        return sysUserMapper.selectOne(new QueryWrapper<SysUser>().eq("username",username));
    }

    @Override
    public Map<String, Object> getUserInfoByUserId(Long userId) {
        //根据用户id查询用户信息
        SysUser sysUser = sysUserMapper.selectById(userId);

        //调用当前实现类中的根据用户id获取用户权限菜单的方法
        List<SysMenu> userMenuList = getUserMenuByUserId(userId);

        //将用户的权限菜单转换为菜单树
        List<SysMenu> userMenuTree = MenuHelper.buildTree(userMenuList);

        //通过RouterHelper工具栏将菜单树转化为路由
        List<RouterVo> userRouters = RouterHelper.buildRouters(userMenuTree);

        //根据用户id获取用户的按钮权限标识符
        List<String> userBtnPers = getUserBtnPersByUserId(userId);
        Map<String,Object> map = new HashMap();
        //获取用户头像地址并放到map中
        map.put("avatar","https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif");
        //获取用户的用户名并放到map中
        map.put("name",sysUser.getUsername());
        //获取用户的权限菜单并将它转换为路由之后放到map中
        map.put("routers", userRouters);
        //获取用户的按钮权限标识符并放到map中
        map.put("buttons", userBtnPers);
        return map;
    }

    @Override
    public List<String> getUserBtnPersByUserId(Long userId) {
        //创建一个返回的List
        List<String> btnPersList = new ArrayList<>();
        //获取用户的权限菜单
        List<SysMenu> userMenus = getUserMenuByUserId(userId);
        //遍历
        for (SysMenu sysMenu : userMenus) {
            if(sysMenu.getType() == 2){
                //获取按钮的权限标识符
                btnPersList.add(sysMenu.getPerms());
            }
        }
        return btnPersList;
    }

    //根据用户的id获取用户权限菜单的方法
    public List<SysMenu> getUserMenuByUserId(Long userId){
        List<SysMenu> userMenuList = null;
        //判断是否是系统管理员
        if(userId == 1){
            //证明是系统管理员，要获取所有的权限菜单
            userMenuList = sysMenuMapper.selectList(new QueryWrapper<SysMenu>().eq("status",1).orderByAsc("sort_value"));
        }else{
            //非系统管理员
            //根据用户id查询用户的权限菜单
            userMenuList = sysMenuMapper.selectUserMenuListByUserId(userId);
        }
        return userMenuList;
    }
}
